<template>
	<view>
		<u-form :model="form" ref="uForm">
			<u-form-item label="姓名" prop="username">
				<u-input v-model="form.username" />
			</u-form-item>
			<u-form-item label="密码" prop="password">
				<u-input v-model="form.password" />
			</u-form-item>
		</u-form>
		<u-button @click="submit">提交</u-button>
	</view>
</template>

<script>
	import userApi from '@/api/user.js'
	export default {
		data() {
			return {
				form: {
					username: 'admin',
					password: '123456',
				},
				rules: {
					username: [{
						required: true,
						message: '请输入姓名',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['blur'],
					}],
					password: [{
						min: 5,
						message: '简介不能少于5个字',
						trigger: 'blur'
					}]
				}
			}
		},
		methods: {
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过');
						userApi.login({
							username:'admin',
							password:"123456"
						}).then((res) => {
							if(res.data.code=='200'&&res.data.success){
								this.$Router.replaceAll({name:'index'})
							}
						})
					} else {
						console.log('验证失败');
					}
				});
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		}
	}
</script>

<style>

</style>
